package cn.tedu.carehome.foundation.controller;



import cn.tedu.carehome.foundation.pojo.dto.OutgoingAddNewDTO;
import cn.tedu.carehome.foundation.pojo.vo.OlderStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.OutgoingListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.OutgoingStandardVO;
import cn.tedu.carehome.foundation.service.IOutgoingService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/outgoings")
@Api(tags = "4.外出申请管理模块")
public class OutgoingController {

    @Autowired
    private IOutgoingService outgoingService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('outgoing/add-new')")
    @ApiOperation("添加外出申请")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid OutgoingAddNewDTO outgoingAddNewDTO){
        log.debug("开始处理添加餐品请求,参数:{}",outgoingAddNewDTO);
        outgoingService.addNew(outgoingAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除申请")
    @PreAuthorize("hasAuthority('outgoing/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "外出申请ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> deleteById(@Range(min = 1, max = 10000, message = "删除餐品失败，餐品ID非法！")
                                 @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除申请】的请求，参数：{}", id);
        outgoingService.deleteById(id);
        return JsonResult.ok();
    }


    @GetMapping("/list")
    @ApiOperation(value="请求申请列表")
    @PreAuthorize("hasAuthority('outgoing/read')")
    @ApiOperationSupport(order = 200)
    public JsonResult<List<OutgoingListItemVO>> list(){
        log.debug("开始处理后台请求外出申请列表");
        List<OutgoingListItemVO> list=outgoingService.list();
        return JsonResult.ok(list);
    }

    @PostMapping("/{id:[0-9]+}/pass")
    @PreAuthorize("hasAuthority('outgoing/update')")
    @ApiOperation("审核通过外出申请")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setPass(@Range(min = 1, max = 1000000, message = "审核通过失败，外出申请ID非法！")
                                    @PathVariable Long id,String checkInfo) {
        log.debug("开始处理【审核外出】的请求，参数：{}", id);
        outgoingService.setPass(id,checkInfo);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/reject")
    @PreAuthorize("hasAuthority('outgoing/update')")
    @ApiOperation("驳回外出申请")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "类别ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> setReject(@Range(min = 1, max = 1000000, message = "审核通过失败，外出申请ID非法！")
                                    @PathVariable Long id,String checkInfo) {
        log.debug("开始处理【审核外出】的请求，参数：{}", id);
        outgoingService.setReject(id,checkInfo);
        return JsonResult.ok();
    }
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询外出申请信息")
    @PreAuthorize("hasAuthority('outgoing/update')")
    @ApiOperationSupport(order = 410)
    public JsonResult<OutgoingStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询外出申请详情】的请求，ID：{}", id);
        OutgoingStandardVO standardVO = outgoingService.getStandardById(id);
        return JsonResult.ok(standardVO);
    }







}
